//
//  YiListView.swift
//  yiplayer
//
//  Created by wkun on 2024/6/12.
//

import UIKit

protocol YIListViewDelegate{
    func handleSelectItem(listView: YiListView, index:Int, model: Any?)
    func handleNaviBack(listView: YiListView)
    func handleNaviRightBtn(listView: YiListView)
    func handleBgView(listView: YiListView)
}

//列表的父类，需要自定义cell才能使用
class YiListView: UIView, UITableViewDataSource, UITableViewDelegate {
    
    let containerView = UIView()
    let navView = YiListNavView()
    let tableView = UITableView()
    var datas: [Any]?
    let reuseid = "rid"
    var delegate: YIListViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func registerCell() {
        self.tableView.register(YiListViewCell.self, forCellReuseIdentifier: self.reuseid)
    }
    
    // MARK: - TouchEvents
    @objc func handleNavRightBtn() {
        delegate?.handleNaviRightBtn(listView: self)
    }
    
    @objc func handleBack() {
        
        if let c = self.currCtrl() {
            c.removeListView()
            
            if c.sublists.count > 1 {
                
            }
        }
        //delegate?.handleNaviBack(listView: self)
    }
    

    @objc func handleBg(){
        //delegate?.handleBgView(listView: self)
//        self.currCtrl()?.sublists.removeAll()
//        self.removeFromSuperview()
        self.handleBack()
    }
    
    // MARK: - TableDelegate
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.datas?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
        return tableView.rowHeight
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let h = self.tableView(tableView, heightForRowAt: indexPath)
        if h <= 0 {
            var cc = tableView.dequeueReusableCell(withIdentifier: "cc")
            if cc == nil {
                cc = UITableViewCell.init(style: .default, reuseIdentifier: "cc")
            }
            return cc!
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: self.reuseid, for: indexPath)
        
        if let c = cell as? YiListViewCell {
            c.model = self.model(datas, indexPath)
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let m = self.model(datas, indexPath)
        delegate?.handleSelectItem(listView: self, index: indexPath.row, model: m)
    }
    
    // MARK: - SetupUI
    func setupUI() {
        
        let blur = BLBlurEffect.getWith(.dark, blurRadius: 10.0)
//        let blur = UIBlurEffect.init(style: .dark)
        let effectView = UIVisualEffectView.init(effect: blur)
        effectView.alpha = 0.8
        effectView.embedIn(self)
        
        let bg = Button.embedIn(self)
        bg.addTarget(self, action: #selector(handleBg), for: .touchUpInside)
        
        containerView.backgroundColor = .clear
        let ch: CGFloat = 310
        let wh: CGFloat = 280
        containerView.pin(.wh(wh, ch)).addTo(self).makeCons { make in
            make.centerX.equal(self).offset(0)
            make.centerY.equal(self).offset(0)
        }
        
        let navH: CGFloat = 40.0
        navView.addTo(containerView).makeCons { make in
            make.top.left.right.equal(0)
            make.height.equal(navH)
        }
        navView.backgroundColor = .init(white: 1.0, alpha: 0.5)
        navView.leftBtn.addTarget(self, action: #selector(handleBack), for: .touchUpInside)
        navView.rightBtn.addTarget(self, action: #selector(handleNavRightBtn), for: .touchUpInside)
        
        self.tableView.rowHeight = 45.0
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.separatorStyle = .none
        self.tableView.backgroundColor = .init(white: 0, alpha: 0.2)
        self.tableView.addTo(self.containerView).makeCons { make in
            make.left.right.equal(0)
//            make.top.equal(self.navView).bottom.equal(0)
            make.top.equal(navH)
            make.height.equal(ch-navH)
        }
    }
    
    // MARK: - homevc
    func currCtrl() -> YiHomeCtrl? {
        return self.getViewController() as? YiHomeCtrl
    }
}

class YiListViewCellModel: NSObject {
    var imgname: String?
    var text: String?
    var model: Any?  //原数据的模型
}


class YiListViewCell: UITableViewCell {
    let imgView = UIImageView()
    let textL = UILabel()
    let line = UIView()
    
    var model: Any?{
        didSet{
            if let m = model as? YiListViewCellModel {
                self.textL.text = m.text
                self.imgView.setImg(m.imgname)
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.backgroundColor = .clear
        self.contentView.backgroundColor = .clear
        
        imgView.pin(.w(18)).mode(.scaleAspectFit).addTo(self.contentView).makeCons { make in
            make.left.equal(15)
//            make.right.equal(-10)
            make.centerY.equal(self.contentView).offset(0)
        }
        
        let lf = 18+15+10
        textL.font(14).color(ColorF).addTo(self.contentView).makeCons { make in
            make.left.equal(lf)
            make.right.equal(-10)
            make.top.bottom.equal(0)
        }
        
        line.bg(ColorLine).pin(.h(0.5)).addTo(self.contentView).makeCons { make in
            make.left.right.bottom.equal(0)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class YiListNavView: UIView {
    let leftBtn = UIButton()
    let titleL = UILabel()
    let rightBtn = UIButton()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let h: CGFloat = 44
        leftBtn.pin(.wh(50, h)).addTo(self).makeCons { make in
            make.left.equal(0)
            make.centerY.equal(self).offset(0)
        }
        leftBtn.img("arrow_51")
        leftBtn.imageView?.pin(.w(10)).mode(.scaleAspectFit)

        
        rightBtn.pin(.wh(50, h)).addTo(self).makeCons { make in
            make.right.equal(0)
            make.centerY.equal(self).offset(0)
        }
        rightBtn.img("home_add")
        rightBtn.imageView?.pin(.w(18)).mode(.scaleAspectFit)
        
        titleL.font("16").color(Color3).align(.center).addTo(self).makeCons { make in
            make.left.equal(self.leftBtn).right.offset(0)
            make.right.equal(self.rightBtn).left.offset(0)
            make.centerY.equal(self).offset(0)
            make.height.equal(30)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


extension YIListViewDelegate{
    func handleSelectItem(listView: YiListView, index:Int, model: Any?){}
    func handleNaviBack(listView: YiListView){}
    func handleNaviRightBtn(listView: YiListView){}
    func handleBgView(listView: YiListView){}
}
